#!/bin/bash
#项目名称
PROJECT_NAME="@projectName@"
PROJECT_GIT_URL="@gitPath@"
GITBRANCH="@branch@"
PROPERTY="@profile@"
TOMCAT_PORT="@tomcatPort@"
DUBBO_PORT="@dubboPort@"
MODULE_NAME="@moduleName@"
FINAL_NAME="@finalName@"
#项目根目录
HOME_ROOT="/data/project/"$PROJECT_NAME
#项目源码
SOURCE_DIR=$HOME_ROOT"/source/"
#项目源码构建目录
PROJECT_DIR=$SOURCE_DIR`ls $SOURCE_DIR`
#tomcat webroot
WEB_ROOT=$HOME_ROOT"/code/"
#tomcat 目录
TOMCAT_HOME=$HOME_ROOT"/tomcat/"
MAVEN_HOME="/data/apache-maven-3.3.9"
update_code(){
        if [ -d $PROJECT_DIR ]
        then
            cd $PROJECT_DIR
           git checkout $GITBRANCH
           git pull
        else
            cd $SOURCE_DIR
            git clone $PROJECT_GIT_URL  
            cd $PROJECT_DIR
            git checkout $GITBRANCH     
        fi
}
install(){
        cd $PROJECT_DIR;
        $MAVEN_HOME/bin/mvn clean; 
        $MAVEN_HOME/bin/mvn -T 1C -Dmaven.test.skip=true  -Dmaven.compile.fork=true -P $PROPERTY install;
}
 
 
deploy(){
        TEMP=$WEB_ROOT"*"
        rm -fR $TEMP
        TEMP=$PROJECT_DIR"/"$MODULE_NAME"/target/"$FINAL_NAME"/*"
        cp -fR $TEMP $WEB_ROOT
}
 
restart(){
	TOMCAT_PID=`jps -v|grep $DUBBO_PORT|awk  '{print $1}' `
	kill -9 $TOMCAT_PID
    sleep 5
    bash $TOMCAT_HOME"bin/catalina.sh" start
}

stop(){
    TOMCAT_PID=`jps -v|grep $DUBBO_PORT|awk  '{print $1}' `
    kill -9 $TOMCAT_PID
    sleep 5
}
 
help(){
        echo $"Usage: $0 {update_code|install|backup|deploy|restart}"
}
 
case "$1" in
stop)
        stop
;;
update_code)
        update_code
;;
install)
        install
;;
deploy)
        deploy
;;
restart)
        restart
;;
-h)
        help
;;
--help)
        help
;;
*)
        update_code
        install
        deploy
        restart
;;
 
esac
 
exit 0
